<?php

/**
 * connexionAppli actions.
 *
 * @package    e-DSL
 * @subpackage connexionAppli
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
 */
class connexionAppliBackActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
    $this->form = new ConnexionAppliForm();
  }
  
  public function executeSubmit($request)
 {
   $ldapActive = sfConfig::get('app_ldapRoot_isactive');
   $params = array('nni'    => $request->getParameter('login'),
    			   'pwd'   => $request->getParameter('pwd'),);

   $nni = strtoupper($params['nni']);
   $pwd = $params['pwd'];
   
   $this->getUser()->setAttribute('nni',$nni);
   if($ldapActive){ 
	   $ldap = new ldap();
	   
	   if (!empty($nni)&& !empty($pwd) && $ldap->rechercheLDAP($nni, $pwd)){
	    //if (!empty($nni)&& !empty($pwd) && $user1->connect($nni, $pwd)){
	    		// '<h1>LDAP ACTIVE</h1>';
	    		$this->getUser()->setAuthenticated(true);
	   			$donnesPersone = $ldap->recupInfosLDAP($nni);
	   			$this->getUser()->setAttribute('nom',$donnesPersone[0]);
	
	   			$personne=Doctrine::getTable('personnes')->findOneByCode($nni);
	   	   	   	if($personne==''){
					//Il faut creer la personne.
					$personne = new personnes;
					$personne->nni = $nni;
				}else{
				}
				$personne->name=$donnesPersone[0];
				$personne->nom=$donnesPersone[1];
				$personne->prenom=$donnesPersone[2];
				$personne->email=$donnesPersone[3];
				$personne->sdum=$donnesPersone[4];
				//TODO : obtener el rol par default sfConfig::get('app_etat_initial_demande')
				$personne->save();
				if ($personne->administrator){
					 $this->getUser()->addCredential('administrator');	
				}else{
					$this->getUser()->setAuthenticated(false);		
				}
		        $referer = $this->getUser()->getAttribute('referer', '@homepage');
		        $this->getUser()->getAttributeHolder()->remove('referer');
		        $this->redirect($referer);				
		   		//$this->redirect('homepage');

	   			
		   }else {

		   		// display the form and set referer
	   		    $this->form = new ConnexionAppliForm();
		   		$this->setSigninReferer();
		   		$this->getUser()->setAuthenticated(false);
		   }
       }else{
	    	// echo '<h1>LDAP INACTIVE</h1>';
       	
       		$personne=Doctrine::getTable('personnes')->findOneByCode($nni);
       	
       		if (!empty($nni)&& !empty($pwd) && !$personne==''){
	    		$this->getUser()->setAuthenticated(true);
	   			$this->getUser()->setAttribute('nom',$personne->name);
	   			if ($personne->administrator){
					 $this->getUser()->addCredential('administrator');	
				}
				
		   		$this->redirect('homepage');
				//$this->redirect('mesdossiers/index');
	   			
		   }else {
		   		//$this->redirect('homepage');
		   		
		   		// display the form and set referer
	   		    $this->form = new ConnexionAppliForm();
		   		$this->setSigninReferer();
		   		$this->getUser()->setAuthenticated(false);
		   }       	
       }
 	}
  	
 	public function executeLogout($request)
	{
		$this->getUser()->setAttribute('nom','');
		$this->getUser()->setAuthenticated(false);
		$this->getUser()->setAttribute('nni','');
		$this->getUser()->getAttributeHolder()->clear();
		$this->form = new ConnexionAppliForm();
	}
  
	private function setSigninReferer(){
      $default_referer = '@homepage';
  
      if ($this->getContext()->getActionStack()->getSize() > 0) {
          $action = $this->getContext()->getActionStack()->getFirstEntry()->getActionInstance();
          $security = $action->getSecurityConfiguration(); 
          $action_name = $this->getContext()->getActionStack()->getFirstEntry()->getActionName();

          // module/config/security.yml action setting takes priority
          if (isset($security[$action_name]['is_secure']) && $security[$action_name]['is_secure']) {
              $referer = $this->getRequest()->getUri();   
          } elseif (isset($security['all']['is_secure']) && $security['all']['is_secure']) {
              $referer = $this->getRequest()->getUri();         
          } else {
              $referer = $default_referer;
          }

      } else {
          $referer = $default_referer;
      }
      
      $this->getUser()->setAttribute('referer', $referer);
  }
	
} 

